package com.dotflat.staffExtend.service.impl;


import com.dotflat.rest.PO.AreableObjectBasePO;
import com.dotflat.rest.service.impl.AreableObjectBaseServiceImpl;
import com.dotflat.staffExtend.domain.StaffExtendInfo;
import com.dotflat.staffExtend.po.*;
import com.dotflat.staffExtend.repository.StaffExtendInfoRepository;
import com.dotflat.staffExtend.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.function.Consumer;

@Service
public class StaffExtendInfoServiceImpl  extends AreableObjectBaseServiceImpl<StaffExtendInfo, StaffExtendInfoRepository>
        implements StaffExtendInfoService {

    @Autowired
    @Override
    public void setBaseRepository(StaffExtendInfoRepository repository){
        super.setBaseRepository(repository);
    }

    @Autowired
    private StaffExtendHealthyService healthyService;

    @Autowired
    private StaffExtendCareerService careerService;

    @Autowired
    private StaffExtendLicenseService licenseService;

    @Autowired
    private StaffExtendRelationService relationService;

    @Autowired
    private StaffExtendVaccineService vaccineService;

    @Override
    public StaffExtendInfo add(String areaId, String staffd, AreableObjectBasePO param, Consumer<? super StaffExtendInfo> preAction){

        if (param instanceof StaffExtendInfoPO){

            StaffExtendInfoPO po = (StaffExtendInfoPO)param;

            List<StaffExtendHealthyPO> healthies = po.getHealthies();
            po.setHealthies(null);

            List<StaffExtendCareerPO> careers = po.getCareers();
            po.setCareers(null);

            List<StaffExtendLicensePO> licenses = po.getLicenses();
            po.setLicenses(null);

            List<StaffExtendRelationPO> relations = po.getRelations();
            po.setRelations(null);

            List<StaffExtendVaccinePO> vaccines = po.getVaccines();
            po.setVaccines(null);

            StaffExtendInfo ret = super.add(areaId, staffd, param, null);

            String extendInfoId = ret.getId();

            if (vaccines != null){
//                vaccineService.deleteAllByExtendInfoId(extendInfoId);
                vaccines.stream().forEach(o-> vaccineService.add(areaId, staffd, o, k->k.setExtendInfoId(extendInfoId)));
            }
            if (relations != null){
//                relationService.deleteAllByExtendInfoId(extendInfoId);
                relations.stream().forEach(o-> relationService.add(areaId, staffd, o, k->k.setExtendInfoId(extendInfoId)));
            }
            if (licenses != null){
//                licenseService.deleteAllByExtendInfoId(extendInfoId);
                licenses.stream().forEach(o-> licenseService.add(areaId, staffd, o, k->k.setExtendInfoId(extendInfoId)));
            }
            if (careers != null){
//                careerService.deleteAllByExtendInfoId(extendInfoId);
                careers.stream().forEach(o-> careerService.add(areaId, staffd, o, k->k.setExtendInfoId(extendInfoId)));
            }
            if (healthies != null){
//                healthyService.deleteAllByExtendInfoId(extendInfoId);
                healthies.stream().forEach(o->healthyService.add(areaId, staffd, o, k->k.setExtendInfoId(extendInfoId)));
            }

            ret = get(areaId, staffd, ret.getId());

            return ret;
        }else {
            StaffExtendInfo ret = super.add(areaId, staffd, param, null);

            return ret;
        }
    }

}
